<?php
	if(!isset($_SESSION)){
		session_start();
	}

	//clear previous messages
	if(isset($_SESSION['error'])) session_unregister('error');
	if(isset($_SESSION['success'])) session_unregister('success');
	
	//get values from the form
	$itemCode=$_POST['itemCode'];
	$itemName=$_POST['itemName'];
	$itemDescription=$_POST['itemDescription'];
	$itemUnit=$_POST['itemUnit'];
	$categoryId=$_POST['categoryId'];
	
	//validation
	if($itemName==""||$itemCode==""||$itemUnit==""||$categoryId==""){
		$_SESSION['error']='Pleace complete the mandatory fields';
	}else{
		include('../dbConnection.php');
		if($result=mysql_query("SELECT * FROM items WHERE itemName='$itemName'")){
			if(mysql_num_rows($result) > 0){
				$row=mysql_fetch_assoc($result);
				$_SESSION['error']=$row['itemName']." is already exist";
			}else{
				if(mysql_query("INSERT INTO items (itemCode, itemName, itemDescription, itemUnit, categoryId) 
				VALUES('$itemCode','$itemName','$itemDescription','$itemUnit','$categoryId')")){
					$_SESSION['success']='Item Added successfully';
				}else{
					$_SESSION['error']='Error Occured';
				}
			}
		}else{
			$_SESSION['error']='Error Occured';
		}
	}
	$_SESSION['pages']='addItems.php';
	header("location: ../../");	
?>